new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Sales_Receipt_SerialNumber: '',//主键
                Receipt_Date: '',//出库日期
                Receipt_Type: "0",//出货地点
                Receipt_Name:"",
                Store_SerialNumber: "",//仓库编号
                Store_Name:"",
                Enterprise_Info_SerialNumber: "",//企业主键
                Children: {
                    sales_receipt_detail: [
                        {
                            Sales_Receipt_Detail_SerialNumber: "",//主键
                            Sales_Receipt_SerialNumber: "",//外键
                            Order_Type: "1",//订单类型
                            Sales_Order_SerialNumber: "",//销售订单流水号
                            Article_SerialNumber: "",//商品流水号
                            Article_Name:"",
                            Receipt_Quantity: "",//出库数量
                            // Amount:"",//商品单价
                            Order_Name:"",
                        }
                    ]
                }
            },
            purchaseOrderData:{},
            formData: {},
            loading: false,
            disabled:true
        }
    },
    mounted() {
        this.formData = JSON.parse(sessionStorage.getItem('fillorder'))
        console.log(this.formData)
        this.form.Receipt_Date = getYMDHMSDate()
        this.form.Receipt_Type == '0' ? this.form.Receipt_Name  = '仓库' : this.form.Receipt_Name  = '店铺'
        this.form.Children.sales_receipt_detail[0].Order_Type == '1' ? this.form.Children.sales_receipt_detail[0].Order_Name = '企业' : this.form.Children.sales_receipt_detail[0].Order_Name = '用户'
        this.form.Store_Name = this.formData.Warehouse_Name
        this.form.Enterprise_Info_SerialNumber = this.formData.Enterprise_Info_SerialNumber
        this.form.Children.sales_receipt_detail[0].Sales_Order_SerialNumber = this.formData.Purchase_Order_SerialNumber
        this.form.Children.sales_receipt_detail[0].Article_Name = this.formData.Article_Name
        this.form.Children.sales_receipt_detail[0].Receipt_Quantity = this.formData.Delivery_Quantity
        if(this.form.Children.sales_receipt_detail.length!=0){
            this.disabled = false
        }
    },
    methods: {
        SubmitChange(row, Row_Status, message = '成功') {//修改查到的原值并提交
            row.Row_Status = Row_Status
            console.log(row)
            let _this = this
            $.ajax({
                type: 'POST',
                url: DomainName + "/getEditAction?token=" + $.cookie("token"),
                data: {
                    Data: JSON.stringify({
                        Modify: { delivery_order: [row] }
                    })
                },
                success: function (res) {
                    console.log(res)
                    if (res.Status == 'success') {
                        console.log(_this.formData.Purchase_Order_SerialNumber)
                        _this.purchaseOrder(_this.formData.Purchase_Order_SerialNumber)
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        SubmitChange2(row, Row_Status) {//修改查到的原值并提交
            row.Row_Status = Row_Status
            console.log(row)
            let _this = this
            $.ajax({
                type: 'POST',
                url: DomainName + "/getEditAction?token=" + $.cookie("token"),
                data: {
                    Data: JSON.stringify({
                        Modify: { purchase_order: [row] }
                    })
                },
                success: function (res) {
                    console.log(res)
                    if (res.Status == 'success') {
                        _this.$message({
                            message: '提交成功',
                            type: 'success'
                          });
                        _this.loading = false
                        window.history.go(-1)
                    }
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        submitForm() {
            let _this = this
            _this.loading = true
            this.form.Receipt_Type = '0'
            this.form.Children.sales_receipt_detail[0].Order_Type = '1'
            this.form.Store_SerialNumber = this.formData.Warehouse_SerialNumber
            this.form.Children.sales_receipt_detail[0].Article_SerialNumber = this.formData.Article_SerialNumber
            console.log(this.form)
            $.ajax({
                type: 'POST',
                url: DomainName + "/setReduceInventoryAction?token=" + $.cookie("token"),
                data: {
                    Data: JSON.stringify({
                        sales_receipt: [_this.form]
                    })
                },
                success: function (res) {
                    console.log(res)
                    if (res.Status == 'success') {
                        _this.SubmitChange(_this.formData, '1', '发货成功')
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        purchaseOrder(Primary_Key) {
            console.log(Primary_Key)
            let _this = this
            $.ajax({
                type: 'POST',
                url: DomainName + "/getInquiryIDOriginal?token=" + $.cookie("token"),
                data: {
                    Primary_Key_Name: 'Purchase_Order_SerialNumber',
                    Primary_Key,
                    Data: 'purchase_order',
                    isNullEnterprise:true
                },
                success: function (res) {
                    console.log(res)
                    _this.purchaseOrderData = res[0]
                    _this.SubmitChange2(_this.purchaseOrderData,'4')
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        }
    },
})